package com.withouther.robot.rainingrobot.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.setting.dialect.Props;
import com.withouther.robot.rainingrobot.core.constants.RoleType;
import com.withouther.robot.rainingrobot.model.WebScoket;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName WebScoketUtils
 * @Auther: tzq
 * @Date: 2020/6/12 09:10
 * @Description:
 */
public class WebScoketUtils {
    private static final String path;
    public static ConcurrentHashMap<String, WebScoket> webScoketMap = new ConcurrentHashMap<String, WebScoket>();
    /**
     * 储存需要播放的语音
     */
    public static ConcurrentHashMap<String, String> userMap = new ConcurrentHashMap<String, String>();
    /**
     * on = 机器人说话 off = 用户说话
     */
    public static ConcurrentHashMap<String, String> intentSwitchMap = new ConcurrentHashMap<String, String>();


    static {
        path = new Props("config.properties").getStr("asr.file.path");
    }
    /**
    * @Author tzq
    * @Description //TODO 获取文件名
    * @Date 11:17 2020/6/12
    * @param id
    * @param type
    * @throws
    * @return java.lang.String
    **/
    public static String getAsrPath(String id, int type) {
        return path + "/" + id + "_" + DateUtil.current(true) + ".pcm";
    }

    public static String getRecordPath(String id, int type) {
        return path + "/" + id + "_" + DateUtil.current(true) + "-record.pcm";
    }

    /**
    * @Author tzq
    * @Description //TODO 存入asr所需文件
    * @Date 11:17 2020/6/12
    * @param id
    * @param type
    * @throws
    * @return boolean
    **/
    public static boolean setAsrFilePath(String id, int type) throws IOException {
        String path = getAsrPath(id, type);
        String key = getScoketKey(id, type);
        if (webScoketMap.get(key) == null) {
            WebScoket webScoket = new WebScoket();

            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();// 能创建多级目录
            }
            webScoket.setFileOutputStream(new FileOutputStream(file, true));
            webScoket.setFilePath(path);
            webScoket.setRecordFile(getRecordPath(id, type));
            webScoketMap.put(key, webScoket);
            return true;
        }
        return false;
    }

    public static String getScoketKey(String id, int type) {
        return id + "_"  + type;
    }
    public static String getScoketSessionKey(String id, int type) {
        switch (type) {
            case RoleType.CLIENT :
                return getScoketKey(id, RoleType.SERVICE);
            case RoleType.SERVICE :
                return getScoketKey(id, RoleType.SERVICE);
            default:
                return getScoketKey(id, RoleType.SERVICE);
        }
    }
    public static WebScoket getAsrWebScoket(String key) {
        return webScoketMap.get(key);
    }
    public static void removeAsrWebScoket(String key) {
        webScoketMap.remove(key);
    }

    /**
     *
     * @Title: writeFile
     * @Description: 字节数组写入文件(追加)
     * @param @param bfile
     * @param @param filePath
     * @param @param fileName    参数
     * @return void    返回类型
     * @throws
     */
    public static void writeFile(byte[] bfile, String filePath) {
        BufferedOutputStream bos = null;
        File file = null;

        try {
            file = new File(filePath);
            bos = new BufferedOutputStream(new FileOutputStream(file, true));
            bos.write(bfile);
            //bos.write("\r\n".getBytes());
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
